import 'dart:convert';

import 'package:http/http.dart' as http;

import '../Robot.dart';
import '../details.dart';
import '../pojo.dart';

class  CommodityViewModel{


  Future<int> login() async {
    print("login start");
    var url = Uri.https(host, 'robot/login');
    var response = await http.post(url, body: {'sn': Robot.sn});
    var a = ApiResponse<LoginBean>.fromJson(jsonDecode(response.body),
            (data) => LoginBean.fromJson(data)); // 传递一个从 JSON 解析 LoginBean 的方法);

    print(a.data.toString());
    if (a.data?.userId == null) {
      print('请扫码');
      return 0;
    }
    Robot.userId = a.data!.userId!;
    Robot.companyId = a.data!.companyId!;
    Robot.accessToken = a.data!.accessToken!;
    print("Robot.accessToken:"+Robot.accessToken);
    print("login end");
    return 1;
  }


  Future<Commodity> getProductById(String id) async {
    var url = Uri.https(host, 'product/commodity/getById');
    var response = await http.post(url, body: {'access_token': Robot.accessToken,'id':id});
    print("getProductById:"+response.body);
    var a = ApiResponse<Commodity>.fromJson(jsonDecode(response.body),  (data) => Commodity.fromJson(data));
    print("getProductById: "+a.data.toString());
    print("getProductById: "+a.data!.name!);
    return a.data!;
  }


}